package leetcode.weekly.week339;

import java.util.Arrays;

//Solution3Test
public class Solution3 {

	public int miceAndCheese(int[] reward1, int[] reward2, int k) {
		int n = reward1.length;
		int[] h = new int[n];
		Integer[] index = new Integer[n];
		for (int i = 0; i < n; i++) {
			h[i] = reward1[i] - reward2[i];
			index[i] = i;
		}
		Arrays.sort(index, (a, b) -> h[b] - h[a]);
		int ans = 0;
		for (int i = 0; i < n; i++, k--) {
			if (k > 0) {
				ans += reward1[index[i]];
			} else {
				ans += reward2[index[i]];
			}
		}
		return ans;
	}

}
